<template>
    <div class="login">
        <div class="login-form">
            <div class="login-form-left"><p></p></div>
            <div class="login-form-right">
                <div class="guan">
                    管理员登录
                </div>
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <el-form-item prop="name" label="管理员名称">
                        <el-input v-model="ruleForm.name"></el-input>
                    </el-form-item>
                    <el-form-item label="管理员密码" prop="password">
                        <el-input v-model="ruleForm.password" type="password"></el-input>
                    </el-form-item>
                    <el-form-item prop="yzm">
                        <img :src="yzm_src" alt="验证码" @click="yzm" class="yzm_img">
                        <el-input v-model="ruleForm.yzm" class="yzm" placeholder="请输入验证码"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button class="btn-login" type="primary" @click="submitForm('ruleForm')">登录</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            //自定义验证规则
            var valudatorYzm = (rule, value, callback) => {
                if (value.length !== 5) {
                    callback(new Error('请输入5位数验证码'));
                }
                callback();
            };
            //属性
            return {
                ruleForm: {
                    name: '',
                    password: '',
                    yzm: ''
                },
                yzm_src: '',
                rules: {
                    name: [
                        { required: true, message: '请输入管理员名称', trigger: 'blur' },
                        { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
                    ],
                    password: [
                        {required: true, message: '请输入管理员密码', trigger: 'blur'},
                        {min: 6, max: 20, message: '长度在6到20个字符', trigger: 'blur'}
                    ],
                    yzm: [
                        {required: true, message: '请输入验证码', trigger: 'blur'},
                        {validator: valudatorYzm, trigger: 'blur'}
                    ]
                }
            };

        },
        created() {
            this.yzm();
        },
        methods: {
            //获取验证码
            yzm () {
                axios.get(this.adminApi('yzm'))
                    .then(res=> {
                        if (res.data.code === 0) {
                            this.yzm_src = res.data.img;
                            console.log(this.yzm_src)
                        } else {
                            this.$message({message: res.data.msg, type: 'error'});
                        }

                    })
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.login_submit();
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            //后台登录提交
            login_submit() {
                axios.post(this.adminApi('login-act'),{
                    name: this.ruleForm.name,
                    password: this.ruleForm.password,
                    yzm: this.ruleForm.yzm
                 })
                 .then(res=> {
                     if (res.data.code === 0) {
                         this.$message({ message: res.data.msg, type: 'success'});
                         this.$router.push({path:'/home'})
                     } else {
                         this.$message({message: res.data.msg, type: 'error'});
                         this.yzm();
                     }

                })
                .catch(err=> {
                    console.log(err)
                })
            }

        }
    }
</script>

<style scoped>
    .login {
        background: url("../../../images/admin/login_bj.jpg") no-repeat;
        background-size: 100% 100%;
        width: 100%;
        height: 100%;
        position: fixed;
        min-width: 1000px;
    }

    .login-form {
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        margin: auto;
        width: 900px;
        height: 350px;
        border: 6px solid rgba(69, 166, 219, .7);
        -ms-border: 6px solid #3ea1db;
        border-radius: 7px;
    }

    .login-form > div {
        float: left;
    }

    .login-form-left {
        width: 415px;
        height: 100%;
        background: #fff url("../../../images/admin/lg-fr-img.png") center no-repeat;
    }

    .login-form-left p {
        float: right;
        width: 22px;
        height: 100%;
        background: url("../../../images/admin/s-t.jpg");
    }

    .login-form-right {
        background-color: #fff;
        width: 473px;
        height: 100%;
        padding: 17px 55px;
        box-sizing: border-box;
    }

    .guan {
        color: #4F94CD	;
        font-size: 25px;
        border-bottom: 1px dotted #4F94CD;
        margin-bottom: 30px;
    }

    .btn-login {
        width: 138%;
        margin-left: -100px;
    }

   .yzm_img {
       margin-left: -100px;
       border: 1px solid #E8E8E8;
       border-radius: 3px;
   }

    .yzm {
        margin-left: 35px;
        width: 200px;
    }
</style>